package com.kehutong.common.schedule;

import java.text.ParseException;

import com.coraframework.quartz.*;
import com.coraframework.quartz.impl.StdSchedulerFactory;

/**
 *
 * @author liuzhen （liuxing521a@163.com）
 * @createTime 2019年12月27日下午3:06:24
 */
public class QuartzManager {
	
	private static String jobGroupName = "notice";
	private static String triggerGroupName = "notice-trigger";
	private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();

	/**
	 * 添加一个定时任务，使用默认的任务组名，触发器名，触发器组名
	 * 
	 * @param jobName 任务名
	 * @param job     任务
	 * @param time    时间设置，参考quartz说明文档
	 * @throws SchedulerException
	 * @throws ParseException
	 */
	public static void addJob(String jobName, Object task, Job job, String time) throws SchedulerException, ParseException {
		addJob(jobName, task, job.getClass(), time);
	}

	public static void addJob(String jobName, Object task, Class<? extends Job> job, String time) throws SchedulerException, ParseException {
		Scheduler sched = schedulerFactory.getScheduler();
		NoticeJobDetail jobDetail = new NoticeJobDetail(jobName, jobGroupName, task, job);// 任务名，任务组，任务执行类
		// 触发器
		CronTrigger trigger = new CronTrigger(jobName, triggerGroupName);// 触发器名,触发器组
		trigger.setCronExpression(time);// 触发器时间设定
		sched.scheduleJob(jobDetail, trigger);
		// 启动
		if (!sched.isShutdown()) {
			sched.start();
		}
	}

//
//	/** */
//	/**
//	 * 添加一个定时任务
//	 * 
//	 * @param jobName          任务名
//	 * @param jobGroupName     任务组名
//	 * @param triggerName      触发器名
//	 * @param triggerGroupName 触发器组名
//	 * @param job              任务
//	 * @param time             时间设置，参考quartz说明文档
//	 * @throws SchedulerException
//	 * @throws ParseException
//	 */
//	public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Job job,
//			String time) throws SchedulerException, ParseException {
//		Scheduler sched = schedulerFactory.getScheduler();
//		JobDetail jobDetail = new JobDetail(jobName, jobGroupName, job.getClass());// 任务名，任务组，任务执行类
//		// 触发器
//		CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组
//		trigger.setCronExpression(time);// 触发器时间设定
//		sched.scheduleJob(jobDetail, trigger);
//		if (sched.isShutdown()) {
//			sched.start();
//		}
//	}

	/** */
	/**
	 * 修改一个任务的触发时间(使用默认的任务组名，触发器名，触发器组名)
	 * 
	 * @param jobName
	 * @param time
	 * @throws SchedulerException
	 * @throws ParseException
	 */
	public static void modifyJobTime(String jobName, String time) throws SchedulerException, ParseException {
		Scheduler sched = schedulerFactory.getScheduler();
		Trigger trigger = sched.getTrigger(jobName, triggerGroupName);
		if (trigger != null) {
			CronTrigger ct = (CronTrigger) trigger;
			if (!ct.getCronExpression().equals(time)) {
				JobDetail jobDetail = sched.getJobDetail(jobName, jobGroupName);
				Class<? extends Job> jobClass = jobDetail.getJobClass();
				removeJob(jobName);
				addJob(jobName, null, jobClass, time);
			}
		}
	}

	/** */
	/**
	 * 修改一个任务的触发时间
	 * 
	 * @param triggerName
	 * @param triggerGroupName
	 * @param time
	 * @throws SchedulerException
	 * @throws ParseException
	 */
	public static void modifyJobTime(String triggerName, String triggerGroupName, String time)
			throws SchedulerException, ParseException {
		Scheduler sched = schedulerFactory.getScheduler();
		Trigger trigger = sched.getTrigger(triggerName, triggerGroupName);
		if (trigger != null) {
			CronTrigger ct = (CronTrigger) trigger;
			// 修改时间
			ct.setCronExpression(time);
			// 重启触发器
			sched.resumeTrigger(triggerName, triggerGroupName);
		}
	}

	/** */
	/**
	 * 移除一个任务(使用默认的任务组名，触发器名，触发器组名)
	 * 
	 * @param jobName
	 * @throws SchedulerException
	 */
	public static void removeJob(String jobName) throws SchedulerException {
		Scheduler sched = schedulerFactory.getScheduler();
		sched.pauseTrigger(jobName, triggerGroupName);// 停止触发器
		sched.unscheduleJob(jobName, triggerGroupName);// 移除触发器
		sched.deleteJob(jobName, jobGroupName);// 删除任务
	}

	/** */
	/**
	 * 移除一个任务
	 * 
	 * @param jobName
	 * @param jobGroupName
	 * @param triggerName
	 * @param triggerGroupName
	 * @throws SchedulerException
	 */
	public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName)
			throws SchedulerException {
		Scheduler sched = schedulerFactory.getScheduler();
		sched.pauseTrigger(triggerName, triggerGroupName);// 停止触发器
		sched.unscheduleJob(triggerName, triggerGroupName);// 移除触发器
		sched.deleteJob(jobName, jobGroupName);// 删除任务
	}
}
